package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysOperLogDO;
import com.andy.blog.dal.mapper.SysOperLogMapper;
import com.andy.blog.dal.service.SysOperLogDalService;
import com.andy.blog.dal.vo.SysOperLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 操作日志 服务层处理
 * <p>
 * Created by andy on 2023-04-05 11:38
 */
@Service
public class SysOperLogDalServiceImpl implements SysOperLogDalService {

    @Resource
    private SysOperLogMapper operLogMapper;


    @Override
    public void insert(SysOperLogVO operLogVO) {
        SysOperLogDO sysOperLogDO = TransformUtils.transform(operLogVO, SysOperLogDO.class);

        sysOperLogDO.setOperTime(new Date());

        try {
            operLogMapper.insert(sysOperLogDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }


    @Override
    public IPage<SysOperLogDO> selectListByPage(SysOperLogVO sysOperLogVO) {
        LambdaQueryWrapper<SysOperLogDO> lambdaQueryWrapper = buildQueryWrapper(sysOperLogVO);

        IPage<SysOperLogDO> page = new Page<>(sysOperLogVO.getPageNum(), sysOperLogVO.getPageSize());
        return operLogMapper.selectPage(page, lambdaQueryWrapper);
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysOperLogDO> buildQueryWrapper(SysOperLogVO sysOperLogVO) {
        LambdaQueryWrapper<SysOperLogDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Objects.nonNull(sysOperLogVO.getBusinessType()), SysOperLogDO::getBusinessType, sysOperLogVO.getBusinessType())
                .like(StringUtils.isNotBlank(sysOperLogVO.getModuleTitle()), SysOperLogDO::getModuleTitle, sysOperLogVO.getModuleTitle())
                .eq(Objects.nonNull(sysOperLogVO.getStatus()), SysOperLogDO::getStatus, sysOperLogVO.getStatus())
                .like(Objects.nonNull(sysOperLogVO.getOperName()), SysOperLogDO::getOperName, sysOperLogVO.getOperName())
                .ge(StringUtils.isNotBlank(sysOperLogVO.getBeginTime()), SysOperLogDO::getOperTime, sysOperLogVO.getBeginTime())
                .le(StringUtils.isNotBlank(sysOperLogVO.getEndTime()), SysOperLogDO::getOperTime, sysOperLogVO.getEndTime())
                .orderByDesc(SysOperLogDO::getId);
        return lambdaQueryWrapper;
    }


    @Override
    public int deleteByIds(List<Long> ids) {
        return operLogMapper.deleteBatchIds(ids);
    }

    @Override
    public void cleanOperLog() {
        operLogMapper.cleanOperLog();
    }

    @Override
    public List<SysOperLogDO> selectListByCondition(SysOperLogVO sysOperLogVO) {
        LambdaQueryWrapper<SysOperLogDO> lambdaQueryWrapper = buildQueryWrapper(sysOperLogVO);
        return operLogMapper.selectList(lambdaQueryWrapper);
    }
}
